﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace Iori.GoogleTranslate {
    internal class AjaxAPI {
        //// <summary> 
        /// 使用WebRequest获取Google翻译后的内容 
        /// </summary> 
        /// <param name="strTranslateString">需要翻译的内容</param> 
        /// <param name="strRequestLanguage">原文语种</param> 
        /// <param name="strResultLanguage">译文语种</param> 
        /// <returns></returns> 
        public static string GetGoogleTranslateJSONString(string strTranslateString, string strRequestLanguage, string strResultLanguage) {
            WebRequest request = HttpWebRequest.Create(
            "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" 
            + strTranslateString + "&langpair=" + strRequestLanguage + "%7C" + strResultLanguage);
            request.Credentials = CredentialCache.DefaultCredentials;
            HttpWebResponse response =
                (HttpWebResponse)request.GetResponse();
            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();
            reader.Close();
            dataStream.Close();
            response.Close();
            return responseFromServer;
        }
    }
}
